//app.js
var util = require('./utils/util.js');
var getInfo = util.getInfo;
App({
    onLaunch: function(options) {
        let that = this;
        that.globalData.pathApp = options.path;
        if (1089 == options.scene) {
            that.globalData.xcx = false
        }
        // 版本更新
        wx.getSystemInfo({
            success: function(res) {
                // console.log(res.SDKVersion)
                if (res.SDKVersion > '1.9 .0') {
                    const updateManager = wx.getUpdateManager()
                    updateManager.onCheckForUpdate(function(res) {
                        // console.log(res.hasUpdate)
                    })
                    updateManager.onUpdateReady(function() {
                            wx.showModal({
                                title: '更新提示',
                                content: '新版本已经准备好，是否马上重启小程序？',
                                success: function(res) {
                                    // console.log(res.confirm)
                                    if (res.confirm) {
                                        updateManager.applyUpdate()
                                    }
                                }
                            })
                        })
                        // 失败
                    updateManager.onUpdateFailed(function() {})
                }
            }
        });
        that.village_LBS();
        // 获取用户信息
        // wx.getSetting({
        //     success: res => {
        //         if (res.authSetting['scope.userInfo'] != undefined && res.authSetting['scope.userInfo'] != true) {} else {
        //             that.ifuserinfo(res);
        //         };
        //         if (options.path != 'pages/reward/reward') {
        //             that.village_LBS()
        //         }
        //     }
        // });
    },
    ifuserinfo() {
        let that = this;
        wx.getSetting({
            success: res => {
                if (res.authSetting['scope.userInfo'] != undefined && res.authSetting['scope.userInfo'] != true) {} else {
                    var _userinfo = wx.getStorageSync('_userinfo');
                    var weapp_openid = wx.getStorageSync('weapp_openid');
                    if (_userinfo && weapp_openid && weapp_openid == _userinfo.openId) {} else {
                        wx.navigateTo({
                            url: '../../pages/accredit/accredit',
                        })
                    }
                }
            }
        });
    },
    getLbs() {
        let that = this;
        wx.getSetting({
            success: res => {
                if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
                    wx.showModal({
                        title: '位置信息授权',
                        content: '位置授权暂未开启动',
                        confirmText: '开启授权',
                        cancelText: '仍然拒绝',
                        success: function(res) {
                            if (res.confirm) {
                                wx.navigateTo({
                                    url: '../../pages/Authorization/Authorization',
                                })
                            } else if (res.cancel) {
                                wx.navigateTo({
                                    url: '../../pages/Authorization/Authorization',
                                })
                            }
                        }
                    })
                }
            }
        })
    },
    village_LBS() {
        let that = this;
        wx.getLocation({
            success(res) {
                that.globalData.latitude = res.latitude
                that.globalData.longitude = res.longitude
            },
            fail(res) {
                wx.getSetting({
                    success(res) {
                        if (res.authSetting['scope.userLocation'] == false) {
                            wx.showModal({
                                title: '是否授权当前位置',
                                content: '需要获取您的地理位置，请确认授权，否则无法试玩',
                                confirmText: '开启授权',
                                cancelText: '仍然拒绝',
                                success: function(res) {
                                    if (res.confirm) {
                                        wx.navigateTo({
                                            url: '../../pages/Authorization/Authorization',
                                        })
                                    } else if (res.cancel) {
                                        wx.navigateTo({
                                            url: '../../pages/Authorization/Authorization',
                                        })
                                    }
                                }
                            })
                        }
                    }
                })
            }
        })
    },
    globalData: {
        isLogin: false, //是否登录
        user: null, //微信获取的用户信息
        userInfo: null, //后台请求回来的用户数据
        commonUrl: 'https://wxb.whwangdoudou.cn/pdd.php', //api地址
        t: '',
        latitude: '',
        longitude: '',
        pathApp: '',
        xcx: false
    },
})